package cn.tedu.exam1.user.service.impl;

import cn.tedu.exam1.base.exception.ServiceException;
import cn.tedu.exam1.base.response.StatusCode;
import cn.tedu.exam1.user.mapper.UserMapper;
import cn.tedu.exam1.user.pojo.dto.UserLoginParam;
import cn.tedu.exam1.user.pojo.dto.UserQuery;
import cn.tedu.exam1.user.pojo.dto.UserRegParam;
import cn.tedu.exam1.user.pojo.dto.UserSaveParam;
import cn.tedu.exam1.user.pojo.entity.User;
import cn.tedu.exam1.user.pojo.vo.UserVO;
import cn.tedu.exam1.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if(user.getId()==null){
            if(userMapper.selectByUsername(user.getUsername())!=null){
                throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
            }
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }else{
           user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public void updateUserAvatar(UserSaveParam userSaveParam) {
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        user.setUpdateTime(new Date());
        userMapper.updateAvatar(user);
    }
    @Override
    public void reg(UserRegParam userRegParam) {
        log.debug("用户注册:userSaveParam" + userRegParam);
        UserVO userVO = userMapper.selectByUsername(userRegParam.getUsername());
        if (userVO!=null){
            throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);

        user.setCreateTime(new Date());
        userMapper.insert(user);
    }
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录:userLoginParam"+userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if (userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        List<UserVO> list = userMapper.selectUser(userQuery);
        return list;
    }

    @Override
    public UserVO selectId(Long id) {
        UserVO userVO = userMapper.selectById(id);
        return userVO;
    }
    @Override
    public void deleteUser(Long userId) {
        userMapper.deleteUser(userId);
    }

}


